package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sql.TeamMemberDao;
import beans.TeamMemberItemBean;
import beans.TeamMembersBean;

/**
 * Servlet implementation class GetTeamMembers
 */
@WebServlet("/GetTeamMembers")
public class GetTeamMembers extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTeamMembers() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * Get Team Members for the Assigned To dropdown for a task
	 * Get Team Members for Team screen for View Current Project
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TeamMemberDao teamMemberDao = new TeamMemberDao();
		HttpSession session = request.getSession(true);	
		TeamMemberItemBean currentUserInfo = (TeamMemberItemBean) session.getAttribute("currentUser");
		TeamMembersBean teamMembersBean = null;
		
		String system = request.getParameter("system");
	
		try {
			if (system == null) {
				teamMembersBean = teamMemberDao.getTeamMembersForProject(currentUserInfo.getProjectId());
			}
			else {
				teamMembersBean = teamMemberDao.getTeamMembersForSystem();
			}
			PrintWriter out = response.getWriter();
			out.write(teamMembersBean.getJson());
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

}
